<template>
	<view class="main">
		<view class="active_tit">
			<view class="tit">
				主要资产情况（房产类）
				
			</view>
			<lb-picker :props='props0' @confirm='confirm0' ref='Picker0' :list='list0'></lb-picker>
			<lb-picker :props='props' @confirm='confirm' ref='Picker1' :list='list'></lb-picker>
			<lb-picker :props='props2' @confirm='confirm2' ref='Picker2' :list='list2'></lb-picker>
		</view>
		<!-- input -->
		<block v-for="(item,index) in form" :key='index' style="margin-bottom: 2rem;">
			<view class="cut">房产{{index+1}}<uni-icons class="clear" type="clear" @click='stdDel(form,item,"stdOffsurCollateral")'></uni-icons></view>
			
			<!-- <ActiveForm required ref=''>
				<view class="active_label">
					房产名称：
				</view>
				<view class="active_input">
					<input type="text" v-model="item.certificateName" />
				</view>
			</ActiveForm> -->
			
			<ActiveForm required ref=''>
				<view class="active_label">
					房产性质：
				</view>
				<view class="active_input">
					<SelectPicker v-model="item.buildingAttr" :options="buildingAttrOption" placeholder="请选择" />
				</view>
			</ActiveForm>
			<!-- <ActiveForm required ref=''>
				<view class="active_label">
					产权证号：
				</view>
				<view class="active_input">
					<input type="text" v-model="item.certificateCode" />
				</view>
			</ActiveForm> -->
			<ActiveForm required ref='mortgageAdd'>
				<view class="active_label">
					资产所在地区：
				</view>
				<view class="active_input">
					<RegionPicker :value="mortgageAdd(item.mortgageAdd)"  :options="region2"  @change="((o)=>{item.mortgageAdd=o.value.toString()})"    />
					<!-- <input type="text" v-model="form.mortgageAdd" /> -->
				</view>
			</ActiveForm>
			
			<ActiveForm required ref='buildingName'>
				<view class="active_label">
					楼盘名称：
				</view>
				<view class="active_input">
					<input type="text" v-model="item.buildingName" placeholder="关键字查询"/>
					<button type="pg" @click="selectXx(index,'buildingName',0)">楼盘查询</button>
				</view>
			</ActiveForm>
			<ActiveForm required ref='buildingNo'>
				<view class="active_label">
					楼栋号（号数）：
				</view>
				<view class="active_input">
					<input type="text" :disabled='zcDisabled' v-model="item.buildingNo" />
				    <button type="pg" @click="selectXx(index,'buildingNo',1)">精准选择</button>
				</view>
			</ActiveForm>
			<ActiveForm required ref='floor'>
				<view class="active_label">
					所在楼层（层数）：
				</view>
				<view class="active_input">
					<input @input="fcInout(item)" type="number" v-model="item.floor" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='floor'>
				<view class="active_label">
					房号（号数）：
				</view>
				<view class="active_input">
					<input :disabled='zcDisabled' type="text" v-model="item.roomNo" />
				<button type="pg" @click="selectXx(index,'roomNo',2)">精准选择</button>
				</view>
			</ActiveForm>
			<ActiveForm required ref='allFloor'>
				<view class="active_label">
					总层数（层数）：
				</view>
				<view class="active_input">
					<input type="number" v-model="item.allFloor" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='area'>
				<view class="active_label">
					面积（平方米）：
				</view>
				<view class="active_input">
					<input @input="fcInout(item)"  type="number" v-model="item.area" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='guaranteeBalance'>
				<view class="active_label">
					贷款余额（元）：
				</view>
				<view class="active_input">
					<mInput placeholder='此套房产的所有抵押贷款' type="number" v-model="item.guaranteeBalance" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='autoAssessPrice'>
				<view class="active_label">
					评估价值（线上）：
				</view>
				<view class="active_input">
					<mInput type="number" disabled v-model="item.autoAssessPrice" />
					<button type="pg" @click="pg(item,index)">线上评估</button>
				</view>
			</ActiveForm>
			<ActiveForm required ref='manAssessPrice'>
				<view class="active_label">
					评估价值（人工）：
				</view>
				<view class="active_input">
					<mInput maxlength="16" type="number" v-model="item.manAssessPrice" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='residue'>
				<view class="active_label">
					剩余价值（元）：
				</view>
				<view class="active_input">
					<mInput maxlength="16" type="number" :disabled="true"  :value="sy(item)" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='residue'>
				<view class="active_label">
					备注：
				</view>
				<view class="active_input">
					<input  v-model="item.remark" />
				</view>
			</ActiveForm>
			<block v-for="(zitem,index) in item.obligeeNameList">
				<view class="cut">权利人{{index+1}}<uni-icons class="clear" type="clear" @click='stdDel(item.obligeeNameList,zitem,"stdOffsurCollateralPledgor")'></uni-icons></view>
				
					<ActiveForm required ref=''>
						<view class="active_label">
							权利人名称：
						</view>
						<view class="active_input">
							<input type="text" v-model="zitem.pledgorName" />
						</view>
					</ActiveForm>
				
			</block>
			<view class="active_tit">
				<view class="tit">
				</view>
				<view class="add" @click="addUser(index)">
					添加权利人
				</view>
			</view>
		</block>
		
		<view class="active_tit">
			<view class="" style="display: flex;">
				<view class="add" @click="add">
					添加资产
				</view>
				
			</view>
			
		</view>
	</view>
</template>

<script>
	import SelectPicker from "../../SelectPicker";
	import RegionPicker from "../../RegionPicker";
	import region from '../areaList.js'
	export default {
		name: 'userAssets',
		data() {
			return {
				region,
				region2:[],
				province: "",
				city: "",
				county: "",
				index: 1,
				form: [],
				buildingAttrOption:[],
				pledgorTypeOption:[],
				idenTypeOption:[],
				pledgorIdenAttOption:[],
				selectIndex:null,
				selectName:'',
				props0: {
					label: 'projectName',
					value: 'projectId'
				},
				props:{label:'buildingName',value:'buildingName'},	
				props2:{label:'houseName',value:'houseName'},	
				list: [],
				list0: [],
				list2: [],
				zcDisabled:true
			}
		},
	
		components: {
			SelectPicker,
			RegionPicker

		},
		computed:{
			mortgageAdd(){
				return function(val){
					//debugger
				        if(val==''){
				        	return []
				        }else{
				        	return val.split(",")
				        }
				      }
			},
			sy(){
				return function(item){
				       item.residue = item.autoAssessPrice!=''&&item.autoAssessPrice!='0'?item.autoAssessPrice-item.guaranteeBalance:item.manAssessPrice-item.guaranteeBalance
					   if(item.residue<0){
					   	return 0
					   }else{
					   	return item.residue
					   }
				      }
			},
		},
		watch: {
			
			form: {
				handler: function(res) {
					//console.warn('stdOffsurCollateral02组件数据变化', res)
					getApp().globalData.stdOffsurCollateral02 = res
				},
				deep: true
			}
		},
		mounted() {
			
			//获取返显数据
			setTimeout(()=>{
				this.region2 = getApp().globalData.FxtData
				//数据反显
				this.buildingAttrOption = getApp().globalData.optType.BUL_ATT
				this.pledgorTypeOption = getApp().globalData.optType.SUBJECT
				this.idenTypeOption = getApp().globalData.optType.PLEDGOR_IDEN
				this.pledgorIdenAttOption = getApp().globalData.optType.PLEDGOR_IDEN_ATT
				let arr = getApp().globalData.stdOffsurCollateral.dyws
				if(arr!=null){
					for(let i=0;i<arr.length;i++){
						if(arr[i].busiType=='02'){
							this.form.push(arr[i])
						}
					}
				}
			},getApp().globalData.timeout)
		},
		methods: {
			fcInout(item){
				
				item.autoAssessPrice = 0
			},
			selectXx(index,name,type){
				
				this.selectIndex = index
				this.selectName = name
				
				//接口评估房产
				let item = this.form[index]
				let arr=item.mortgageAdd.split(',')
				console.log(arr)
				if (type == 0) {
					if (item.mortgageAdd == '') {
						uni.showToast({
							icon: 'none',
							title: '请先选择资产所在地',
							duration: 2000
						})
						return false
					}
					if (item.buildingName == '') {
						uni.showToast({
							icon: 'none',
							title: '请先输入楼盘关键字',
							duration: 2000
						})
						return false
					}
				
					let data = {
						"areaId": arr[2],
						"cityId": arr[1],
						"key": item.buildingName
					}
					this.sendRequest({
						url: 'fxt/project',
						method: 'post',
						hideLoading: true,
						data: data,
						complete: (res) => {
							if (res.code == '0') {
								this.list0 = res.body
								this.$refs.Picker0.show() // 显示
							} else {
								//this.zcDisabled = false
								uni.showToast({
									icon: 'none',
									title: '楼盘获取失败，请重试',
									duration: 2000
								})
							}
				
						}
					})
				
				} else if(type==1){
					  if(item.mortgageAdd==''){
					  	uni.showToast({
					  		icon:'none',
					  		title:'请先选择资产所在地',
					  		duration:2000
					  	})
					  	return false
					  }
					  if(item.buildingName==''){
					  	uni.showToast({
					  		icon:'none',
					  		title:'请先输入楼盘名称',
					  		duration:2000
					  	})
					  	return false
					  }
					  let data = {
					      "areaId":arr[2],
					      "buildingName": item.buildingNo,
					      "cityId": arr[1],
					      "floor": item.floor,
					      "houseName": item.roomNo,
					      "projectName": item.buildingName,
						  "projectId": item.projectId
					    }
					  this.sendRequest({
					  	url: 'fxt/building',
					  	method: 'post',
					  	hideLoading: true,
					  	data: data,
					  	complete: (res) => {
					  		if(res.code=='0'){
								this.list = res.body
								this.$refs.Picker1.show() // 显示
							}else{
								//this.zcDisabled = false
								uni.showToast({
									icon:'none',
									title:'楼栋号获取失败,请重试',
									duration:2000
								})
							}
					  		
					  	}
					  }) 
				  }else{
					  if(!item.buildingId){
					  	uni.showToast({
					  		icon:'none',
					  		title:'请精准选择获取楼栋号',
					  		duration:2000
					  	})
					  	return false
					  }
					  if(!item.floor){
					  	uni.showToast({
					  		icon:'none',
					  		title:'请输入楼层',
					  		duration:2000
					  	})
					  	return false
					  }
					  let data = {
						  "cityId": arr[1],
						  "buildingId": item.buildingId,
						  "floor": item.floor,
					      "areaId":arr[2],
					      "buildingName": item.buildingNo,
					    }
					  this.sendRequest({
					  	url: 'fxt/house',
					  	method: 'post',
					  	hideLoading: true,
					  	data: data,
					  	complete: (res) => {
					  		if(res.code=='0'){
					  			this.list2 = res.body
					  			this.$refs.Picker2.show() // 显示
					  		}else{
								//this.zcDisabled = false
					  			uni.showToast({
					  				icon:'none',
					  				title:'房号获取失败,请重试',
					  				duration:2000
					  			})
					  		}
					  		
					  	}
					  }) 
				  }
				
				
				
				//this.$refs.picker.hide() // 隐藏
			},
			confirm0(e) {
				console.log(e)
				this.form[this.selectIndex][this.selectName] = e.item.projectName
				this.form[this.selectIndex].projectId = e.item.projectId
			},
			confirm(e){
				this.form[this.selectIndex].projectId = this.list[e.index].projectId
				this.form[this.selectIndex].buildingId = this.list[e.index].buildingId
				this.form[this.selectIndex][this.selectName] = e.value
			},
			confirm2(e){
				this.form[this.selectIndex].houseId = this.list2[e.index].houseId
				this.form[this.selectIndex][this.selectName] = e.value
			},
			pg(item,index){
				//接口评估房产
				let arr=item.mortgageAdd.split(',')
				let data = {  
				    "areaId":arr[2],
				    "buildingName": item.buildingNo,
				    "cityId": arr[1],
				    "floor": item.floor,
				    "houseName": item.roomNo,
				    "projectName": item.buildingName,
					buildArea:item.area,
					projectId:item.projectId,
					buildingId:item.buildingId,
					houseId:item.buildingId
				  }
				this.sendRequest({
					url: 'fxt/enquiry',
					method: 'post',
					hideLoading: true,
					data: data,
					complete: (res) => {
						
						if (res.code == '0') {
						   item.autoAssessPrice = res.body.totalPrice
						}else{
							uni.showToast({
								icon:'none',
								title:res.message||"评估失败",
								duration:2000
							})
						}
					}
				})  
			},
			addUser(index){
				
				if(this.form[index].obligeeNameList==undefined){
					this.form[index].obligeeNameList = []
				}
				this.form[index].obligeeNameList.push(
					{
						idenCode:'',//证件号码
						idenType:'',//证件类型
						pledgorIdenAtt:'',//抵押人身份属性
						pledgorName:'',//抵押人名称
						pledgorType:'',//抵押人类型
						postCode:'',//邮政编码
						residence:'',//住所
						telephone:'',//联系电话
						tranAdd:''//通讯地址
					}
				)
			},
			add(){
				let obj = {
					mortagageCount: '',
					busiType: '02',
					corCerttype: '',
					checked: '',
					mortgageAdd: '',
					buildingName: '',
					buildingNo:'',
					certificateAdd: '',
					certificateCode: '',
					area: '',
					floor: '',
					allFloor: '',
					buildingAttr: '',
					fixStatus: '',
					certificateRegDate: '',
					certificateEndDate: '',
					obligeeNameList:[
						{
							idenCode:'',//证件号码
							idenType:'',//证件类型
							pledgorIdenAtt:'',//抵押人身份属性
							pledgorName:'',//抵押人名称
							pledgorType:'',//抵押人类型
							postCode:'',//邮政编码
							residence:'',//住所
							telephone:'',//联系电话
							tranAdd:''//通讯地址
						},
					], //多个权利人名称
					idCode: '',
					guaranteeBalance: '',
					residue: '',
					roomNo:'',
					consignor: '',
					consignorPhone: '',
					autoAssessPrice: '',
					manAssessPrice: '',
				}
				this.form.push(obj)
			},
			addChange(o){
				
			}
		},
		
	}
</script>

<style lang="scss">


</style>
